
var _name = document.querySelectorAll("input")[0];
var pass = document.querySelectorAll("input")[1];
var rpass = document.querySelectorAll("input")[2];
var nick = document.querySelectorAll("input")[3];
var btn = document.querySelector("button");
var _text = document.querySelector("span")

btn.addEventListener("click" , function(){
    // 1、创建ajax对象
    let xhr = new XMLHttpRequest();

    // 2、设定open();
    xhr.open("post" , "http://localhost:8888/users/register");

    xhr.setRequestHeader("content-type" , "application/x-www-form-urlencoded");

    // 3、设定send();
    xhr.send(`username=${_name.value}&password=${pass.value}&rpassword=${rpass.value}&nickname=${nick.value}`);

    // 4.接受请求结果
    xhr.onload = () => {
        let result = JSON.parse(xhr.response); 
        console.log(result);
        if(result.code === 1){
            _text.innerHTML = `注册成功将在5秒后跳转到登录页`;
            let time = 1;
            setInterval(function(){
                time--;
                if(time === 0){
                    window.location.href = "./登录框.html";
                    return;
                }
                _text.innerHTML = `注册成功将在${time}秒后跳转到登录页`;
            },1000)
           
        }else if(result.code === 0){
            _text.innerHTML = `注册失败`;
        }
        
    }
})

